#include "stdafx.h"
#include "HexFrame.h"
#include "globals.h"
#include "table.h"
#include "mainwindow.h"

CHexFrame::CHexFrame()
{
	CFrame::CFrame();

	cCharXDim=9;
	cCharYDim=12;
}

void CHexFrame::OnCreate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static BUTTON_REF butref[]=
	{
		16,16,
		IDM_EDIT_CUT,6,
		IDM_EDIT_COPY,7,
		IDM_EDIT_PASTE,8,
		IDM_SCRIPT_DUMP,14,
		IDM_REPLACE_SCRIPT,17,
	};
	m_Menu.LoadBitmap(MAKEINTRESOURCE(IDB_MAINTOOLBAR32));
	m_Menu.SetButtonRef(butref,sizeof(butref)/sizeof(*butref));
}

void CHexFrame::OnSetFocus()
{
	CreateCaret (*this, NULL, 12, 14);
	SetCaretPos(320,240);
	ShowCaret(*this);
}

void CHexFrame::OnKillFocus()
{
	DestroyCaret();
}

void CHexFrame::Clear(HDC hdc, COLORREF color)
{
	RECT rect;
	HBRUSH brush=(HBRUSH)CreateSolidBrush(color);
	GetClientRect(hWnd,&rect);

	int ret=FillRect(hdc, &rect, brush);
	DeleteObject(brush);
}

void CHexFrame::DrawBorder(HDC hdc)
{
	//RECT rect;
	//GetClientRect(hWnd,&rect);
	//DrawEdge(hdc, &rect, EDGE_SUNKEN, BF_RECT);
}

void CHexFrame::Draw(HDC hdc)
{
	extern Translhextion mainwindow;

	HideCaret (hWnd);
	int b;
	b = min (iCurrentLine + cBufferY, iNumlines-1);
	COLORREF iBkColor =		PALETTERGB (GetRValue(m_colors[cc_background]),GetGValue(m_colors[cc_background]),GetBValue(m_colors[cc_background]));
	COLORREF iTextColor =	PALETTERGB (GetRValue(m_colors[cc_text]),GetGValue(m_colors[cc_text]),GetBValue(m_colors[cc_text]));
	COLORREF iTextColorE =	PALETTERGB (GetRValue(m_colors[cc_text_e]),GetGValue(m_colors[cc_text_e]),GetBValue(m_colors[cc_text_e]));
	COLORREF iTextColorO =	PALETTERGB (GetRValue(m_colors[cc_text_o]),GetGValue(m_colors[cc_text_o]),GetBValue(m_colors[cc_text_o]));

	TCHAR *lBuffer=new TCHAR[iCharsPerLine];

	//SetTextColor (hdc, iTextColor);
	//SetBkColor (hdc, iBkColor);
	////HPEN pen1 = CreatePen (PS_SOLID, 1, iBkColor);
	////HPEN oldpen = (HPEN) SelectObject (hdc, pen1);
	////CBrush brush1;
	////brush1.CreateSolidBrush (iBkColor);
	////HBRUSH oldbrush = (HBRUSH) SelectObject (hdc, brush1);

	CMemDC mDC(hdc,hWnd,false);
	GString line;

	Clear(mDC,RGB(255,255,255)/*iBkColor*/);
	HFONT oldFont=(HFONT)SelectObject(mDC,hFont);
	SetTextColor(mDC,iTextColor);

	line=_T("Offset");
	TextOut(mDC,0,0,line,line.GetLength());
	SelectObject(mDC,oldFont);

	////HBRUSH hbr = CreateSolidBrush( iBookmarkColor );
	////if (iUpdateLine == -1)
	//	//for (a = iCurrentLine; a <= b; a++)
	//		//PrintLine( hdc, a, lBuffer, hbr );
	////else
	//	//PrintLine( hdc, iUpdateLine, lBuffer, hbr );

	////DeleteObject( hbr );
	////SelectObject (hdc, oldpen);
	////DeleteObject (sep_pen);
	////SelectObject (hdc, oldfont);
	////if(!bThingyView)
	////{
	////	invert_char (hdc);
	////}
	////else
	////{
	////	if (iUpdateLine == -1)
	////	{
	////		thingy_view_invert_char (hdc,a);
	////	}
	////	else
	////	{
	////		thingy_view_invert_char (hdc,iUpdateLine);
	////	}
	////}
	////draw_border_client_rect (hdc);
	
	////EndPaint (hwnd, &ps);
	//place_cursor ();
	////update_window_status ();
	////iUpdateLine = -1;

	//DrawBorder(hdc);

	delete[] lBuffer;
}

void CHexFrame::PrintLine(HDC hdc, int iLine, TCHAR* ucLineBuf, HBRUSH hbr)
{
	_tstring output;
	int totallength = 0;
	std::vector<int> vAsciiLengths;
	if (iLine < iCurrentLine || iLine > iCurrentLine + cBufferY) return;
	int iStartPos = iLine * iBytesPerLine, iEndPos, i = 0, m;
	TCHAR cBuf[80], c;

	if( iStartPos > FileBuffer->Upper() + 1 ) return;

	_stprintf (cBuf, _T("%%%d.%dX     "), iOffsetLength, iOffsetLength);

	////for (m=0; m<iByteSpace; m++)
	////	cBuf[5+m] = ' ';
	////cBuf[5+m] = '\0';

	_stprintf (ucLineBuf, cBuf, iStartPos);
	if (iStartPos+iBytesPerLine > FileBuffer->Length ()) iEndPos = FileBuffer->Upper()+1;
	else iEndPos = iStartPos+iBytesPerLine-1;

	if( iEndPos < iStartPos ) iEndPos = iStartPos;
	m = iOffsetLength+iByteSpace; 
	_tstring dtile;
	for (i=iStartPos; i<=iEndPos; i++)
	{
		if (i == FileBuffer->Length())
		{
			ucLineBuf[m++] = '_';
			ucLineBuf[m++] = '_';
			ucLineBuf[m++] = ' ';
		}
		else
		{
			char temp;
			c = (*FileBuffer[i] >> 4);
			temp = (char) c;
			_strupr(&temp);
			c = (int) temp;
			if( c < 10 )
				c += '0';
			else
				c = c - 10 + 'A';
			ucLineBuf[m++] = c;
			c = (*FileBuffer[i] & 0x0f);
			if( c < 10 )
				c += '0';
			else
				c = c - 10 + 'A';
			ucLineBuf[m++] = c;
			ucLineBuf[m++] = ' ';
			if(vTableOut[(unsigned char) *FileBuffer[i]].length() == 2)
				vDTEs[i] = COLORDTE;
			if(bHighlight)
			{
				dtile.clear();
				if(i + 1 < (int)vChanges.size())
				{
					dtile += (unsigned char) *FileBuffer[i];
					dtile += (unsigned char) *FileBuffer[i+1];
					if(vTwoByteHighlightTable[dualtileindex(dtile)] == L"1")
					{
						vHighlights[i] = COLORHIGHLIGHT;
						vHighlights[i + 1] = COLORHIGHLIGHT;
					}
					else if(vTwoByteHighlightTable[(unsigned char) *FileBuffer[i]] == L"1")
						vHighlights[i] = COLORHIGHLIGHT;
				}
				else
					if(vTwoByteHighlightTable[(unsigned char) *FileBuffer[i]] == L"1")
						vHighlights[i] = COLORHIGHLIGHT;
			}
		}
	}
	if (iEndPos-iStartPos < iBytesPerLine-1)
	{
		for (i=0; i<iBytesPerLine-1-(iEndPos-iStartPos); i++)
		{
			ucLineBuf[m++] = ' ';
			ucLineBuf[m++] = ' ';
			ucLineBuf[m++] = ' ';
		}
	}

	//for (i=0; i<iCharSpace; i++)
	//	ucLineBuf[m++] = ' ';
	//
	//unsigned char * temp;
	//int k = 0;
	//if(!bThingyView)
	//{
	//	for (i=iStartPos; i<=iEndPos; i++)
	//	{
	//		temp = &(FileBuffer[i]);
	//		if (i == FileBuffer.Length())
	//		{
	//			ucLineBuf[m++] = ' ';
	//		}
	//		//else if (iCharacterSet == OEM_FIXED_FONT && FileBuffer[i]!=0)
	//		//{
	//		//	ucLineBuf[m++] = *temp;
	//		//}
	//		else if ((FileBuffer[i]>=32 && FileBuffer[i]<=126) || (FileBuffer[i]>=160 && FileBuffer[i]<=255) || (FileBuffer[i]>=145 && FileBuffer[i]<=146))
	//		{
	//			ucLineBuf[m++] = *temp;
	//		}
	//		else
	//		{
	//			ucLineBuf[m++] = '.';
	//		}
	//	}

	//}
	//else
	//{
	//	std::vector<_tstring> dsize(iEndPos - iStartPos + 1);
	//	vPLineBuf = dsize;
	//	dsize = vEmpty;
	//	for (i=iStartPos; i<=iEndPos; i++)
	//	{
	//		if (i == FileBuffer.Length())
	//		{
	//			;
	//		}
	//		else
	//		{
	//			vPLineBuf[i - iStartPos] = hex_char(FileBuffer[i]);
	//		}
	//	}
	//	bool oldbytemark = bByteMark;
	//	bByteMark = false;
	//	bSplitMultiByte = false;
	//	table.translate(vPLineBuf);
	//	bSplitMultiByte = true;
	//	bByteMark = oldbytemark;
	//	if(i == FileBuffer.Length())
	//	{
	//		vPLineBuf.push_back(L" ");
	//	}
	//	output = L"";
	//	vector<int> dsize2(vPLineBuf.size());
	//	vAsciiLengths = dsize2;
	//	dsize2 = viEmpty;

	//	// draw a line?
	//	for(i = 0;i<(int)vPLineBuf.size();i++)
	//	{
	//		if(vPLineBuf[i] == L"\n")
	//		{
	//			output += L"*";
	//			vAsciiLengths[i] = 1;
	//		}
	//		else if(vPLineBuf[i].length() >= 4 && vPLineBuf[i].substr(1,3) == L"END")
	//		{
	//			output += L"\\";
	//			vAsciiLengths[i] = 1;
	//		}
	//		else if(vPLineBuf[i].substr(0,1) == sByteOpen && vPLineBuf[i].length() == 4) //what about double bytes that translate to {xx}? triples?
	//		{
	//			output += L"#";
	//			vAsciiLengths[i] = 1;
	//		}
	//		else
	//		{
	//			if(vPLineBuf[i].substr(0,1) == sByteOpen)
	//			{
	//				output += vPLineBuf[i].substr(1,vPLineBuf[i].length()-2); 
	//				vAsciiLengths[i] = vPLineBuf[i].length()-2;
	//			}
	//			else
	//			{
	//				if(bJapanese && vPLineBuf[i].length() == 2)
	//				{
	//					_tstring hex = hex_char(vPLineBuf[i][0]) + hex_char(vPLineBuf[i][1]);
	//					long code = dualbyteindex(hex);
	//					TCHAR ascii = -1;

	//					// decode sjis
	//					if(bJPShift)
	//					{
	//						ascii = jpfont_shift(code);
	//					}
	//					// copy utf16
	//					else
	//					{
	//						ascii = (TCHAR)code;
	//						//ascii = jpfont_euc(code);
	//					}

	//					if(ascii != -1)
	//					{
	//						vPLineBuf[i] = L"";
	//						vPLineBuf[i] += ascii;
	//						output += vPLineBuf[i];
	//					}
	//					else
	//					{
	//						output += vPLineBuf[i]; 
	//					}
	//					
	//				}
	//				else if(bJapanese && vPLineBuf[i].length() == 4)
	//				{
	//					_tstring hex = hex_char(vPLineBuf[i][0]) + hex_char(vPLineBuf[i][1]);
	//					long code1 = dualbyteindex(hex);
	//					hex = hex_char(vPLineBuf[i][2]) + hex_char(vPLineBuf[i][3]);
	//					long code2 = dualbyteindex(hex);
	//					TCHAR ascii = -1;
	//					TCHAR ascii2 = -1;
	//					if(bJPShift)
	//					{
	//						ascii = jpfont_shift(code1);
	//						ascii2 = jpfont_shift(code2);
	//					}
	//					else
	//					{
	//						ascii = (TCHAR)code1;
	//						ascii2 = (TCHAR)code2;
	//						//ascii = jpfont_euc(code1);
	//						//ascii2 = jpfont_euc(code2);
	//					}
	//					if(ascii != -1)
	//					{
	//						vPLineBuf[i] = L"";
	//						vPLineBuf[i] += ascii;
	//						//vPLineBuf[i] += ' ';
	//						if(ascii2 != -1)
	//						{
	//							vPLineBuf[i] += ascii2;
	//							//vPLineBuf[i] += ' ';
	//						}
	//						output += vPLineBuf[i];
	//					}
	//					else
	//					{
	//						output += vPLineBuf[i]; 
	//					}
	//					
	//				}
	//				else
	//				{
	//					output += vPLineBuf[i]; 
	//				}
	//				vAsciiLengths[i] = vPLineBuf[i].length();
	//			}
	//		}
	//	}
	//	if((int)output.length() < iMaxCharLine)
	//	{
	//		i = output.length();
	//		if(bHideHex)
	//			i = i - 15;
	//		for(i = i; i < iMaxCharLine;i++)
	//			output += L" ";
	//	}
	//	else
	//	{//NOT GREAT BB
	//		iLastMaxCharLine = iMaxCharLine;
	//		iMaxCharLine = 2 * output.length();
	//	}
	//}
	//if (iEndPos-iStartPos < iBytesPerLine-1)
	//	for (i=0; i<iBytesPerLine-1-(iEndPos-iStartPos); i++)
	//		ucLineBuf[m++] = ' ';
	//
	//
	//int iLineLen = m; 

	//iBkColor = PALETTERGB (GetRValue(iBkColorValue),GetGValue(iBkColorValue),GetBValue(iBkColorValue));
	//iTextColor = PALETTERGB (GetRValue(iTextColorValue),GetGValue(iTextColorValue),GetBValue(iTextColorValue));
	//iTextColorE = PALETTERGB (GetRValue(iTextColorEValue),GetGValue(iTextColorEValue),GetBValue(iTextColorEValue));
	//iTextColorO = PALETTERGB (GetRValue(iTextColorOValue),GetGValue(iTextColorOValue),GetBValue(iTextColorOValue));
	//SetTextColor (hdc, iTextColor);
	//SetBkColor (hdc, iBkColor);

	//if( iHorizontalScrollPos < iOffsetLength + iByteSpace )
	//{
	//	TextOut( hdc,
	//		BORDER_CLIENT_XDIM,
	//		cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//		ucLineBuf + iHorizontalScrollPos, iOffsetLength + iByteSpace - iHorizontalScrollPos );
	//}

	//int iHexStart = iOffsetLength + iByteSpace;
	//int iHexXStart = iHexStart * cCharXDim;

	//if( iHorizontalScrollPos < iOffsetLength + iByteSpace + iBytesPerLine * iHexWidth )
	//{
	//	TextOut( hdc,
	//		BORDER_CLIENT_XDIM + ( iHexStart + iBytesPerLine * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//		cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//		ucLineBuf + iHexStart + iBytesPerLine * iHexWidth,
	//		iCharSpace );
	//}

	//iSelBkColor = PALETTERGB (GetRValue(iSelBkColorValue),GetGValue(iSelBkColorValue),GetBValue(iSelBkColorValue));
	//iSelTextColor = PALETTERGB (GetRValue(iSelTextColorValue),GetGValue(iSelTextColorValue),GetBValue(iSelTextColorValue));
	//BOOL bLastTextNorm = TRUE;
	//COLORREF crefOldColor = GetTextColor(hdc);
	//int p = 0, el = iStartPos + iBytesPerLine - 1 - 1, s, e;
	//for( p = iStartPos; p <= el; p++ )
	//{
	//	s = iOffsetLength + iByteSpace + ( p - iStartPos + 1 ) * iHexWidth;
	//	e = s - iHexWidth;
	//	if( iHorizontalScrollPos < s && iHorizontalScrollPos + cxBuffer >= e && !bHideHex)
	//	{
	//		if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection) )
	//		{
	//			if( bLastTextNorm )
	//			{
	//				//crefOldColor = iSelTextColor;
	//				SetTextColor (hdc, iSelTextColor);
	//				SetBkColor (hdc, iSelBkColor);
	//				bLastTextNorm = FALSE;
	//			}
	//			else
	//			{
	//				SetTextColor (hdc, iSelTextColor);
	//				SetBkColor (hdc, iSelBkColor);
	//			}
	//		}
	//		else
	//		{
	//			if(bLastTextNorm)
	//			{
	//				
	//				if(p % 2 == 0)
	//					SetTextColor(hdc,iTextColorE);
	//				else if(p % 2 != 0)
	//					SetTextColor(hdc,iTextColorO);
	//			}
	//			else
	//			{
	//				if(p % 2 == 0)
	//					SetTextColor(hdc,iTextColorE);
	//				else if(p % 2 != 0)
	//					SetTextColor(hdc,iTextColorO);
	//				SetBkColor (hdc, iBkColor);
	//			}
	//		}
	//		if(p < (int)vChanges.size())
	//		{
	//			if(vChanges[p] == COLORCHANGE)
	//				SetTextColor(hdc,iChangesColorValue);
	//			//else if(vChanges[p] == COLORHIGHLIGHT)
	//			//	SetTextColor(hdc,iHighlightColorValue);
	//			else if(vHighlights[p] == COLORHIGHLIGHT)
	//				SetTextColor(hdc,iHighlightColorValue);
	//		}
	//		if( bSelected && p == iEndOfSelection)
	//		{
	//			TextOut( hdc,
	//			BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//			cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//			ucLineBuf + iHexStart + ( p - iStartPos ) * iHexWidth,
	//			iHexWidth - 1 );
	//		}
	//		else
	//		{
	//			TextOut( hdc,
	//			BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//			cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//			ucLineBuf + iHexStart + ( p - iStartPos ) * iHexWidth,
	//			iHexWidth );
	//		}
	//		
	//		SetTextColor(hdc,crefOldColor);
	//	}
	//	if(!bHideHex)
	//	{
	//		s = iOffsetLength + iByteSpace + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos + 1);
	//	}
	//	else
	//	{
	//		s = iOffsetLength + iByteSpace /*+ iBytesPerLine * iHexWidth + iCharSpace*/ + ( p - iStartPos + 1);
	//	}
	//	if( iHorizontalScrollPos < s && iHorizontalScrollPos + cxBuffer >= s - 1 )
	//	{
	//		if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection) )
	//		{
	//			if( bLastTextNorm )
	//			{
	//				SetTextColor (hdc, iSelTextColor);
	//				SetBkColor (hdc, iSelBkColor);
	//				bLastTextNorm = FALSE;
	//			}
	//			else
	//			{
	//				SetTextColor (hdc, iSelTextColor);
	//				SetBkColor (hdc, iSelBkColor);
	//			}
	//		}
	//		else
	//		{
	//			if( !bLastTextNorm )
	//			{
	//				SetTextColor (hdc, iTextColor);
	//				SetBkColor (hdc, iBkColor);
	//				bLastTextNorm = TRUE;
	//			}
	//			else
	//			{
	//				SetTextColor (hdc, iTextColor);
	//				SetBkColor (hdc, iBkColor);
	//			}
	//		}
	//		if(p < (int)vChanges.size())
	//		{
	//			if(vChanges[p] == COLORCHANGE)
	//				SetTextColor(hdc,iChangesColorValue);
	//			//else if(vChanges[p] == COLORHIGHLIGHT)
	//			else if(vHighlights[p] == COLORHIGHLIGHT)
	//				SetTextColor(hdc,iHighlightColorValue);
	//			//else if(vChanges[p] == COLORDTE)
	//			else if(vDTEs[p] == COLORDTE)
	//				SetTextColor(hdc,iDTEColorValue);
	//		}

	//		// render using ascii encoding
	//		if(!bThingyView)
	//		{
	//			if(!bHideHex)
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ) - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ),
	//				1 );
	//			}
	//			else
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/ + ( p - iStartPos ) - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ),
	//				1 );
	//			}
	//		}
	//		// otherwise use a table
	//		else
	//		{
	//			if(bJapanese)
	//			{
	//				//GetObject(hdc,GetObject( hdc,0,NULL),hfOld); 
	//				SelectObject( hdc, hfJap);
	//			}
	//			if(p-iStartPos < (int)vAsciiLengths.size())
	//			{
	//				LPCTSTR l=output.c_str();

	//				if(!bHideHex)
	//				{
	//					TextOut( hdc,
	//					BORDER_CLIENT_XDIM + ( totallength + iHexStart + iBytesPerLine * iHexWidth + iCharSpace  - iHorizontalScrollPos ) * cCharXDim,
	//					cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//					&l[totallength] /*(output.substr(totallength,vAsciiLengths[p-iStartPos])).c_str()*/,
	//					vAsciiLengths[p - iStartPos]);
	//					totallength += vAsciiLengths[p-iStartPos];
	//				}
	//				else
	//				{
	//					TextOut( hdc,
	//					BORDER_CLIENT_XDIM + ( totallength + iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/  - iHorizontalScrollPos ) * cCharXDim,
	//					cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//					&l[totallength] /*(output.substr(totallength,vAsciiLengths[p-iStartPos])).c_str()*/,
	//					vAsciiLengths[p - iStartPos]);
	//					totallength += vAsciiLengths[p-iStartPos];
	//				}
	//			}
	//			if(p + 1 > el)
	//			{
	//				if(!bHideHex)
	//				{
	//					TextOut( hdc,
	//					BORDER_CLIENT_XDIM + ( totallength + iHexStart + iBytesPerLine * iHexWidth + iCharSpace  - iHorizontalScrollPos ) * cCharXDim,
	//					cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//					(output.substr(totallength,output.length()-totallength)).c_str(),
	//					output.length()-totallength);
	//				}
	//				else
	//				{
	//					TextOut( hdc,
	//					BORDER_CLIENT_XDIM + ( totallength + iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/  - iHorizontalScrollPos ) * cCharXDim,
	//					cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//					(output.substr(totallength,output.length()-totallength)).c_str(),
	//					output.length()-totallength);
	//				}
	//			}
	//			if(bJapanese)
	//			{
	//				//DeleteObject( hdc);
	//				//SelectObject( hdc, hfOld);			
	//				make_font();
	//				SelectObject(hdc, hFont);
	//			}
	//		}
	//	}
	//}
	//s = iOffsetLength + iByteSpace + ( p - iStartPos + 1 ) * iHexWidth;
	//e = s - iHexWidth;
	//if( iHorizontalScrollPos < s && iHorizontalScrollPos + cxBuffer >= e && !bHideHex)
	//{
	//	if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
	//	{
	//		if( bLastTextNorm )
	//		{
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim + 2 * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				_T(" "),
	//				1 );
	//			crefOldColor = iSelTextColor;
	//			SetTextColor (hdc, iSelTextColor);
	//			SetBkColor (hdc, iSelBkColor);
	//			if(p < (int)vChanges.size())
	//			{
	//				if(vChanges[p] == COLORCHANGE)
	//					SetTextColor(hdc,iChangesColorValue);
	//				//else if(vChanges[p] == COLORHIGHLIGHT)
	//				else if(vHighlights[p] == COLORHIGHLIGHT)
	//					SetTextColor(hdc,iHighlightColorValue);
	//			}
	//			bLastTextNorm = FALSE;
	//			bLastTextNorm = TRUE;
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + ( p - iStartPos ) * iHexWidth,
	//				iHexWidth - 1 );
	//		}
	//		else
	//		{
	//			crefOldColor = iSelTextColor;
	//			SetTextColor (hdc, iSelTextColor);
	//			SetBkColor (hdc, iSelBkColor);
	//			if(p < (int)vChanges.size())
	//			{
	//				if(vChanges[p] == COLORCHANGE)
	//					SetTextColor(hdc,iChangesColorValue);
	//				//else if(vChanges[p] == COLORHIGHLIGHT)
	//				else if(vHighlights[p] == COLORHIGHLIGHT)
	//					SetTextColor(hdc,iHighlightColorValue);
	//			}
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + ( p - iStartPos ) * iHexWidth,
	//				iHexWidth - 1 );

	//			SetTextColor (hdc, iTextColor);
	//			crefOldColor = iTextColor;
	//			SetBkColor (hdc, iBkColor);
	//			bLastTextNorm = TRUE;
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim + 2 * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				_T(" "),
	//				1 );
	//		}
	//	}
	//	else
	//	{
	//		if( !bLastTextNorm )
	//		{
	//			if(p < (int)vChanges.size() && vChanges[p] == COLORCHANGE)
	//				SetTextColor(hdc,iChangesColorValue);
	//			else if(p < (int)vChanges.size() && vHighlights[p] == COLORHIGHLIGHT)
	//				SetTextColor(hdc,iHighlightColorValue);
	//			else if(p % 2 == 0)
	//				SetTextColor(hdc,iTextColorE);
	//			else if(p % 2 != 0)
	//				SetTextColor(hdc,iTextColorO);
	//			crefOldColor = iTextColor;
	//			SetBkColor (hdc, iBkColor);
	//			bLastTextNorm = TRUE;
	//		}
	//		else
	//		{
	//			crefOldColor = GetTextColor(hdc);
	//			if(p < (int)vChanges.size() && vChanges[p] == COLORCHANGE)
	//				SetTextColor(hdc,iChangesColorValue);
	//			else if(p < (int)vChanges.size() && vHighlights[p] == COLORHIGHLIGHT)
	//				SetTextColor(hdc,iHighlightColorValue);
	//			else if(p % 2 == 0)
	//				SetTextColor(hdc,iTextColorE);
	//			else if(p % 2 != 0)
	//				SetTextColor(hdc,iTextColorO);	
	//		}
	//		TextOut( hdc,
	//			BORDER_CLIENT_XDIM + ( iHexStart + ( p - iStartPos ) * iHexWidth - iHorizontalScrollPos ) * cCharXDim,
	//			cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//			ucLineBuf + iHexStart + ( p - iStartPos ) * iHexWidth,
	//			iHexWidth );
	//		SetTextColor(hdc,crefOldColor);
	//	}
	//}

	//if(!bHideHex)//problem here I think
	//{
	//	s = iOffsetLength + iByteSpace + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos + 1);
	//}
	//else
	//{
	//	s = iOffsetLength + iByteSpace /*+ iBytesPerLine * iHexWidth* + iCharSpace*/ + ( p - iStartPos + 1);
	//}
	//if( iHorizontalScrollPos < s && iHorizontalScrollPos + cxBuffer >= s - 1 )
	//{
	//	if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
	//	{
	//		if( bLastTextNorm )
	//		{
	//			SetTextColor (hdc, iSelTextColor);
	//			SetBkColor (hdc, iSelBkColor);
	//		}
	//		else
	//		{
	//			SetTextColor (hdc, iSelTextColor);
	//			SetBkColor (hdc, iSelBkColor);
	//		}
	//	}
	//	else
	//	{
	//		if( !bLastTextNorm )
	//		{
	//			SetTextColor (hdc, iTextColor);
	//			SetBkColor (hdc, iBkColor);
	//		}
	//		else
	//		{
	//			SetTextColor (hdc, iTextColor);
	//			SetBkColor (hdc, iBkColor);
	//		}
	//	}
	//	if(p < (int)vChanges.size())
	//	{
	//		if(vChanges[p] == COLORCHANGE)
	//			SetTextColor(hdc,iChangesColorValue);
	//		//else if(vChanges[p] == COLORHIGHLIGHT)
	//		else if(vHighlights[p] == COLORHIGHLIGHT)
	//			SetTextColor(hdc,iHighlightColorValue);
	//		//else if(vChanges[p] == COLORDTE)
	//		else if(vDTEs[p] == COLORDTE)
	//			SetTextColor(hdc,iDTEColorValue);
	//	}
	//	if(!bThingyView)
	//	{
	//		if(!bHideHex)
	//		{
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ) - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ),
	//				1 );
	//		}
	//		else
	//		{
	//			TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/ + ( p - iStartPos ) - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				ucLineBuf + iHexStart + iBytesPerLine * iHexWidth + iCharSpace + ( p - iStartPos ),
	//				1 );
	//		}
	//	}
	//	else
	//	{
	//		if(bJapanese)
	//		{
	//			//GetObject(hdc,GetObject( hdc,0,NULL),hfOld); 
	//			SelectObject( hdc, hfJap);
	//		}
	//		if(p-iStartPos < (int)vAsciiLengths.size())
	//		{
	//			if(!bHideHex)
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( totallength + iHexStart + iBytesPerLine * iHexWidth + iCharSpace  - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				(output.substr(totallength,vAsciiLengths[p-iStartPos])).c_str(),
	//				vAsciiLengths[p - iStartPos]);
	//				totallength += vAsciiLengths[p-iStartPos];
	//			}
	//			else
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( totallength + iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/  - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				(output.substr(totallength,vAsciiLengths[p-iStartPos])).c_str(),
	//				vAsciiLengths[p - iStartPos]);
	//				totallength += vAsciiLengths[p-iStartPos];
	//			}
	//		}
	//		if(p + 1 > el)
	//		{
	//			if(!bHideHex)
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( totallength + iHexStart + iBytesPerLine * iHexWidth + iCharSpace  - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				(output.substr(totallength,output.length()-totallength)).c_str(),
	//				output.length()-totallength);
	//			}
	//			else
	//			{
	//				TextOut( hdc,
	//				BORDER_CLIENT_XDIM + ( totallength + iHexStart /*+ iBytesPerLine * iHexWidth + iCharSpace*/  - iHorizontalScrollPos ) * cCharXDim,
	//				cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//				(output.substr(totallength,output.length()-totallength)).c_str(),
	//				output.length()-totallength);
	//			}
	//		}
	//		if(bJapanese)
	//		{
	//			//SelectObject( hdc, hfOld);	
	//			make_font();
	//			SelectObject(hdc,hFont);
	//			//DeleteObject( hdc );
	//		}
	//		
	//	}
	//}

	//if(bHideHex)
	//{
	//	m = (iOffsetLength+iByteSpace)*cCharXDim - cCharXDim/2 + 3*cCharXDim*4*0 - cCharXDim*iHorizontalScrollPos;
	//	MoveToEx (hdc,
	//		BORDER_CLIENT_XDIM+m,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine)*cCharYDim, NULL);
	//	LineTo (hdc,
	//		BORDER_CLIENT_XDIM+m,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine+1)*cCharYDim);
	//}
	//else
	//{
	//	for (i = 0; i < (iBytesPerLine / 4) + 1; i++)
	//	{
	//		m = (iOffsetLength+iByteSpace)*cCharXDim - cCharXDim/2 + 3*cCharXDim*4*i - cCharXDim*iHorizontalScrollPos;
	//		MoveToEx (hdc,
	//			BORDER_CLIENT_XDIM+m,
	//			cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine)*cCharYDim, NULL);
	//		LineTo (hdc,
	//			BORDER_CLIENT_XDIM+m,
	//			cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine+1)*cCharYDim);
	//	}
	//}

	//if(!bHideHex)
	//{
	//	m = (iOffsetLength + iByteSpace + iBytesPerLine * 3 + iCharSpace)*cCharXDim - cCharXDim*iHorizontalScrollPos - 2;
	//	MoveToEx (hdc,
	//		BORDER_CLIENT_XDIM+m,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine)*cCharYDim, NULL);
	//	LineTo (hdc,
	//		BORDER_CLIENT_XDIM+m,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine+1)*cCharYDim);
	//	MoveToEx (hdc,
	//		BORDER_CLIENT_XDIM+m+2,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine)*cCharYDim, NULL);
	//	LineTo (hdc,
	//		BORDER_CLIENT_XDIM+m+2,
	//		cClientYPos + BORDER_CLIENT_XDIM+(iLine-iCurrentLine+1)*cCharYDim);
	//}
	//el = iStartPos + iBytesPerLine - 1;
	//int iCharCoordinate;
	//RECT r;
	//long bookmark = 0;
	//for( i = 0; i < iBookmarkCount && !bHideHex; i++ )
	//{
	//	bookmark = pbmkList[i].offset;
	//	if(iOffsetMode == LOROM)
	//		bookmark = fromLOROM(bookmark);
	//	else if(iOffsetMode == HIROM)
	//		bookmark = fromHIROM(bookmark);
	//	if( IN_BOUNDS( bookmark, iStartPos, el ) && bookmark <= FileBuffer.Upper() )
	//	{
	//		iCharCoordinate = iOffsetLength + iByteSpace + ( bookmark % iBytesPerLine )*3 - iHorizontalScrollPos;
	//		r.left = BORDER_CLIENT_XDIM + iCharCoordinate * cCharXDim;
	//		r.top = cClientYPos + BORDER_CLIENT_XDIM + ( bookmark / iBytesPerLine - iCurrentLine ) * cCharYDim;
	//		r.right = BORDER_CLIENT_XDIM + r.left + 2*cCharXDim;
	//		r.bottom = cClientYPos + BORDER_CLIENT_XDIM + ( bookmark / iBytesPerLine - iCurrentLine + 1 ) * cCharYDim;
	//		FrameRect( hdc, &r, hbr );

	//		iCharCoordinate = iOffsetLength + iByteSpace + iBytesPerLine*3 + iCharSpace
	//			+ ( bookmark % iBytesPerLine ) - iHorizontalScrollPos;
	//		r.left = BORDER_CLIENT_XDIM + iCharCoordinate * cCharXDim;
	//		r.top = cClientYPos + BORDER_CLIENT_XDIM + ( bookmark / iBytesPerLine - iCurrentLine) * cCharYDim;
	//		r.right = BORDER_CLIENT_XDIM + ( iCharCoordinate + 1 ) * cCharXDim;
	//		r.bottom = cClientYPos + BORDER_CLIENT_XDIM + ( bookmark / iBytesPerLine - iCurrentLine + 1 ) * cCharYDim;
	//		if(!bThingyView)
	//		{
	//			FrameRect( hdc, &r, hbr );
	//		}
	//	}
	//}
	//if(bHideHex)
	//{
	//	int j = 0;
	//	_tstring blank;
	//	for(j = 0;j < iBytesPerLine*4;j++)
	//		blank += L" ";
	//	TextOut( hdc,
	//		BORDER_CLIENT_XDIM + ( totallength + iHexStart + iBytesPerLine /** iHexWidth + iCharSpace*/  - iHorizontalScrollPos ) * cCharXDim,
	//		cClientYPos + BORDER_CLIENT_XDIM + ( iLine - iCurrentLine ) * cCharYDim,
	//	blank.c_str(),
	//	blank.length());
	//}
	//return;
}


void CHexFrame::OnMouseMove(int xPos, int yPos)
{
	iMouseX = xPos;
	iMouseY = yPos;

	if (bLButtonIsDown == FALSE) return;

	bSelected = TRUE;
	if (GetCapture() == NULL)
	{
		SetCapture (hWnd);
		SetCursor( LoadCursor( NULL, IDC_IBEAM ) );
	}

	int iColumnOnScreen = xPos / cCharXDim,
		iRowOnScreen = yPos / cCharYDim,
		iColumn_logical = iColumnOnScreen + iHorizontalScrollPos,
		iLine = iCurrentLine + iRowOnScreen;

	if (yPos>cBufferY*cCharYDim || yPos<0)
	{
		if (bTimerSet==FALSE)
		{
			SetTimer (hWnd, iTimerID, iTimerLength, NULL);
			bTimerSet = TRUE;
		}
	}
	else if (iColumn_logical >= iOffsetLength + iByteSpace && iColumn_logical <= iCharsPerLine - 1 - iBytesPerLine - iCharSpace - 1)
	{
		KillTimer (hWnd, iTimerID);
		bTimerSet = FALSE;
		if ((iColumn_logical-iOffsetLength-(iByteSpace-1))%3 != 0)
		{
			int bytenum = (iColumn_logical-iOffsetLength-(iByteSpace-1))/3,
				iNumOfNib = ((iColumn_logical-iOffsetLength-(iByteSpace-1))%3)-1;
			int newpos = iLine * iBytesPerLine + bytenum;
			if (newpos <= (FileBuffer->Upper()))
			{
				iEndOfSelection = newpos;
				//repaint ();
			}
		}
	}
	else if (iColumn_logical > iCharsPerLine - 1 - iBytesPerLine	&& iColumn_logical < iCharsPerLine)
	{
		KillTimer (hWnd, iTimerID);
		bTimerSet = FALSE;
		int newpos = iLine * iBytesPerLine + (iColumn_logical - (iCharsPerLine - iBytesPerLine));
		if (newpos <= (FileBuffer->Upper ()))
		{
			iEndOfSelection = newpos;
			//repaint();
		}
	}
}

void CHexFrame::OnLButtonUp(int xPos, int yPos)
{
	//SetFocus();

	KillTimer (hWnd, iTimerID);
	bTimerSet = FALSE;
	SetCursor( LoadCursor( NULL, IDC_ARROW ) );
	ReleaseCapture ();
	bLButtonIsDown = FALSE;

	if (iLBDownX == xPos && iLBDownY == yPos)
	{
		int iColumnOnScreen = xPos / cCharXDim, 
			iRowOnScreen = yPos / cCharYDim,	
			iColumn_logical = iColumnOnScreen + iHorizontalScrollPos,	
			iLine = iCurrentLine + iRowOnScreen; 

		if (iColumn_logical >= iOffsetLength + iByteSpace && iColumn_logical <= iCharsPerLine - 1 - iBytesPerLine - iCharSpace - 1)
		{
			if ((iColumn_logical-iOffsetLength-(iByteSpace-1))%3 != 0)
			{
				int bytenum = (iColumn_logical-iOffsetLength-(iByteSpace-1))/3,
					iNumOfNib = ((iColumn_logical-iOffsetLength-(iByteSpace-1))%3)-1;
				int newpos = iLine * iBytesPerLine + bytenum;
				if( newpos <= (FileBuffer->Upper ()) + 1 )
				{
					iCurrentByte = newpos;
					iCurrentNibble = iNumOfNib;
					iTextEntryMode = BYTES;
					//repaint ();
				}
			}
		}
		else if (!bThingyView && iColumn_logical > iCharsPerLine - 1 - iBytesPerLine	&& iColumn_logical < iCharsPerLine)
		{
			int newpos = iLine * iBytesPerLine + (iColumn_logical - (iCharsPerLine - iBytesPerLine));
			if( newpos <= (FileBuffer->Upper ()) + 1 )
			{
				iCurrentByte = newpos;
				iTextEntryMode = CHARS;
				//repaint ();
			}
		}
	}
}

void CHexFrame::OnRButtonUp(int xPos, int yPos)
{
	extern Translhextion mainwindow;

	KillTimer (hWnd, iTimerID);
	bTimerSet = FALSE;
	SetCursor( LoadCursor( NULL, IDC_ARROW ) );
	ReleaseCapture ();
	int iColumnOnScreen = xPos / cCharXDim, 
		iRowOnScreen = yPos / cCharYDim,	
		iColumn_logical = iColumnOnScreen + iHorizontalScrollPos,	
		iLine = iCurrentLine + iRowOnScreen; 
	//HMENU context = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_RIGHTCLICKMENU));
	//HMENU context = CreatePopupMenu();
	CMenu context;
	context.CreatePopupMenu();
	context.AppendMenu(MF_STRING, IDM_EDIT_CUT, _T("Cu&t"));
	if(bThingyView) context.AppendMenu(MF_STRING,IDM_EDIT_COPY, _T("Thingy &Copy"));
	else context.AppendMenu(MF_STRING, IDM_EDIT_COPY, _T("&Copy"));
	context.AppendMenu(MF_STRING, IDM_EDIT_PASTE, _T("&Paste"));
	context.AppendMenu(MF_STRING, IDM_PASTE_WITH_DLG, _T("P&aste preview"));
	if((bTableLoaded && bTableActive && FileBuffer->Length() > 0 ) ||
		(bTableLoaded && bTableActive && !bReadOnly) || ( FileBuffer->Length() > 0 ))
		context.AppendMenu(MF_SEPARATOR, 0, NULL);
	context.AppendMenu(MF_STRING, IDM_SCRIPT_DUMP, _T("&Dump Script..."));
	context.AppendMenu(MF_STRING, IDM_REPLACE_SCRIPT, _T("&Replace Script..."));
	context.AppendMenu(MF_STRING, IDM_COPY_HEXDUMP, _T("H&exdump..."));
	if(( FileBuffer->Length() > 0 )) context.AppendMenu(MF_SEPARATOR, 0, NULL);
	context.AppendMenu (MF_STRING, IDM_GO_TO, _T("&Jump To..."));
	if(( FileBuffer->Length() > 0 )) context.AppendMenu(MF_SEPARATOR, 0, NULL);
	context.AppendMenu(MF_STRING, IDM_SELECT_ALL, _T("Select &all"));

	if( ( bSelected || iCurrentByte <= FileBuffer->Upper() ) && !bReadOnly && !bPartialOpen ) ;
	else context.RemoveMenu(IDM_EDIT_CUT,MF_BYCOMMAND); //CUT
	if( bSelected || iCurrentByte <= FileBuffer->Upper() );
	else context.RemoveMenu(IDM_EDIT_COPY,MF_BYCOMMAND); //COPY

	if( !bReadOnly && !bSelected )
	{
		if( OpenClipboard( NULL ) )
		{
			HGLOBAL hClipMemory = GetClipboardData( CF_UNICODETEXT );
			if( hClipMemory != NULL );
			else context.RemoveMenu(IDM_EDIT_PASTE,MF_BYCOMMAND); //PASTE
			CloseClipboard ();
		}
		// Clipboard could not be opened => can't paste.
		else context.RemoveMenu(IDM_EDIT_PASTE,MF_BYCOMMAND); //PASTE
	}
	else context.RemoveMenu(IDM_EDIT_PASTE,MF_BYCOMMAND); //PASTE
	if( !bReadOnly && !bSelected );
	else context.RemoveMenu(IDM_PASTE_WITH_DLG,MF_BYCOMMAND); //PASTE PREVIEW
	if(bTableLoaded && bTableActive && FileBuffer->Length() > 0 );
	else context.RemoveMenu(IDM_SCRIPT_DUMP,MF_BYCOMMAND);//DUMP SCRIPT
	if(bTableLoaded && bTableActive && !bReadOnly);
	else context.RemoveMenu(IDM_REPLACE_SCRIPT,MF_BYCOMMAND);//REPLACE SCRIPT
	if( FileBuffer->Length()<=0)
	{
		context.RemoveMenu(IDM_COPY_HEXDUMP,MF_BYCOMMAND);	//HEXDUMP
		context.RemoveMenu(IDM_GO_TO,MF_BYCOMMAND);			//JUMP TO
		context.RemoveMenu(IDM_SELECT_ALL,MF_BYCOMMAND);	//SELECT ALL
	}
	
	POINT pt;
	pt.x = xPos;
	pt.y = yPos;
	ClientToScreen(hWnd, &pt); 
	UINT Msg = TrackPopupMenu(context,TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN,pt.x,pt.y,0,hWnd,0);
	mainwindow.OnCommand(Msg,0);
}

void CHexFrame::OnInitMenuPopUp(HMENU hMenu, LPARAM lParam)
{
	m_Menu.OnInitMenuPopup(hMenu/*,lParam,false*/,TRUE);
}

void CHexFrame::OnDrawItem(LPDRAWITEMSTRUCT lpmis)
{
	m_Menu.OnDrawItem(lpmis);
}

void CHexFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpmis)
{
	m_Menu.OnMeasureItem(nIDCtl, lpmis);
}

void CHexFrame::OnVScroll(UINT nSBCode, UINT nPos)
{
	switch (nSBCode)
	{
	case SB_LINEUP:
		VscrollPos -= 1;
		break;
	case SB_LINEDOWN:
		VscrollPos += 1;
		break;
	case SB_PAGEUP:
		VscrollPos -= ClientSize.cy / cCharYDim;
		break;
	case SB_PAGEDOWN:
		VscrollPos += ClientSize.cy / cCharYDim;
		break;
	case SB_THUMBPOSITION:
		VscrollPos = nPos;
		break;
	default:
		break;
	}

	VscrollPos = max(0, min(VscrollPos, 30 - 1));

	if (VscrollPos != GetScrollPos(*this, SB_VERT))
	{
		SetScrollPos(*this, SB_VERT, VscrollPos, TRUE);
		Invalidate();
	}
}

void CHexFrame::OnHScroll(UINT nSBCode, UINT nPos)
{
}
